﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using SdmxMl.Helper;
using SdmxMl.Common;

namespace SdmxMl.Structure
{
    /// <summary>
    /// KeyFamilyRefType provides a reference to a key-family (data set structure definition). At a minimum, either
    /// (a) The key family ID must be provided, as assigned to the key family by the agency whose ID is the value
    /// of KeyFamilyAgencyID. A version must also be provided; OR (b) a valid SDMX Registry URN must be provided
    /// in the URN element (see SDMX Registry Specification)
    /// </summary>
    public class KeyFamilyRefType : ArtefactRefBase
    {
        #region Construction

        public KeyFamilyRefType()
        {
            Id = string.Empty;
            AgencyId = string.Empty;
            Version = string.Empty;
        }

        public KeyFamilyRefType(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr)
        {
            if (nsmgr.VersionSdmx == SdmxVersion.SDMX_20)
            {
                Id = Xmlhelper.GetNodeText(nodeArtefact, "./structure:KeyFamilyID", nsmgr);
                AgencyId = Xmlhelper.GetNodeText(nodeArtefact, "./structure:KeyFamilyAgencyID", nsmgr);
                Version = Xmlhelper.GetNodeText(nodeArtefact, "./structure:Version", nsmgr);

                Urn = Xmlhelper.GetNodeText(nodeArtefact, "./structure:URN", nsmgr);
            }
            else
            {
                XmlNode ndParent = nodeArtefact.SelectSingleNode("./Ref", nsmgr);
                if (ndParent != null)
                {
                    Id = Xmlhelper.GetAtt(ndParent, "id");
                    AgencyId = Xmlhelper.GetAtt(ndParent, "agencyID");
                    Version = Xmlhelper.GetAtt(ndParent, "version");
                }
                else
                {
                    // TODO ny URN
                }

            }
        }

        #endregion //Construction

        #region XML Representation

        public void WriteXml(XmlTextWriter xw, string prefix, string tag)
        {
            // Nothing to do if not specified
            if (string.IsNullOrEmpty(Id) && string.IsNullOrEmpty(Urn))
                return;

            if (SdmxNamespaceManager.SdmxTarget == SdmxVersion.SDMX_21)
            {
                xw.WriteStartElement(prefix, "Structure", null);

                xw.WriteStartElement("Ref");
                xw.WriteAttributeString("agencyID", AgencyId);
                xw.WriteAttributeString("id", Id);
                if (Version != "1.0")
                    xw.WriteAttributeString("version", Version);
                xw.WriteEndElement();

                xw.WriteEndElement();
            }
            else
            {
                xw.WriteStartElement(prefix, tag, null);

                if (string.IsNullOrEmpty(Urn))
                {
                    Xmlhelper.WriteElementIfExist(xw, prefix, "KeyFamilyID", Id);
                    Xmlhelper.WriteElementIfExist(xw, prefix, "KeyFamilyAgencyID", AgencyId);
                    Xmlhelper.WriteElementIfExist(xw, prefix, "Version", Version);
                }
                else
                    Xmlhelper.WriteElementIfExist(xw, prefix, "URN", Urn);

                xw.WriteEndElement();
            }
        }

        #endregion //XMl Representation

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Id).Append(" - V.").Append(Version).Append(" - ").Append(AgencyId);
            return sb.ToString(); 
        }
    }

    public class KeyFamilyRefTypeList : BaseSetlistRefList
    {
    }

}
